<template>
    <div v-if="type==0" class="left" :style="{ height: show? 'calc(100vh - 36px)' :'50px' }">
        <div class="left-header">
            <img class="left-header-back" src="@/assets/images/back.png" @click="emits('back')" />
            <img class="left-header-save" src="@/assets/images/save.png" @click="emits('save')" />
            <div >
                <el-input v-if="showInput" @blur="showInput = false" @keyup.enter.native="showInput = false" placeholder="请输入名称" v-model="name" class="left-header-input" />
                <div v-else class="name" @click="showInputChange">{{ name }}</div>
            </div>
            <div class="left-header-pack" @click="handlePack">
                <img src="@/assets/images/pack.png" />
            </div>

        </div>      
            <div v-if="show" :class="show?'slide-in-left':'slide-out-left'">
                <div class="divider"></div>
                <div class="left-tabs">
                    <div :class="leftTabs === 0?'left-tabs-active':'left-tabs-item' " @click="handleLeftTabs(0)">模型</div>
                    <div :class="leftTabs === 1?'left-tabs-active':'left-tabs-item' " @click="handleLeftTabs(1)">对象</div>
                </div>
                <div class="divider"></div>

                <div class="model" v-if="leftTabs == 0">
                    <div class="model-type">
                        <div v-for="(item,index) in leftToolbarList" :key="item.typeId" :class="selectedType === index ? 'model-type-active' : 'model-type-item'" @click="handleTypeClick(index)">
                            <div class="img">
                                <img :src="item.imgPath" />
                            </div>
                            <div class="name">{{ item.typeName }}</div>
                            <div style="width: 100%;height: 100%;position: absolute;top: 0;"></div>
                        </div>
                    </div>
                    <div class="model-equipment">
                        <el-collapse v-model="activeNames">
                            <el-collapse-item v-for=" item in curManufacturers" :key="item.id" :title="item.manufactorName" :name="item.id" icon="CaretRight">
                                <template #icon="{ isActive }">
                                    <span style="color: #ff0;">
                                        Expanded
                                    </span>
                                </template>
                                <div class="model-equipment-list">
                                    <div v-for=" e in item.equipments" :key="e.id" class="node-item" draggable="true" @dragstart="handleToolbarDragStart(e, $event)" @dragend="handleToolbarDragEnd">
                                        <div class="img">
                                            <img :src="e.imgPath" />
                                        </div>
                                        <div class="name">{{ e.equipmentName }}</div>
                                    </div>
                                </div>
                            </el-collapse-item>
                        </el-collapse>
                    </div>
                </div>
                <div class="left-list" v-else>
                    <div v-for="item in nodeList" :key="item.id" :class="selectedNode?.id == item.id?'left-list-active':'left-list-item'" @click="nodeClick(item)">
                        {{ item.label }}
                    </div>
                    <!-- <el-empty v-if="nodeList.length == 0" description="无对象" /> -->
                </div>
            </div>
        
    </div>
    <div v-else class="left" :style="{ height: '50px' ,width:'60px'}">
        <div class="left-header" style="justify-content: space-between;">
            <img class="left-header-back" src="@/assets/images/back.png" @click="emits('back')" />
            <!-- <div class="left-header-pack" @click="handlePack" >
                <img  src="@/assets/images/pack.png" />

            </div> -->
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
import { getCurrentInstance } from 'vue'
const router = useRouter();
const { proxy } = getCurrentInstance();
const name = defineModel('name');
const show = defineModel('show');
const props = defineProps({
    nodeList: {
        type: Array,
        default: () => []
    },
    leftToolbarList: {
        type: Array,
        default: () => []
    },
    selectedNode: {
        type: Object,
        default: () => null
    },
    type: {
        type: Number,
        default: 0 //1只显示返回和收起
    },

})
const emits = defineEmits(['dragStart', 'dragEnd', 'nodeClick', 'save', 'back','pack'])
const selectedType = ref(0);
const curManufacturers = ref([]); //当前选中厂商
const activeNames = ref("")//当前选中的厂商id
const leftTabs = ref(0);
const showInput = ref(false);
watch(() => props.leftToolbarList, val => {
    //默认选中第一个厂商
    if (val.length) {
        curManufacturers.value = val[0].equipmentManufactorVos;
        if (curManufacturers.value.length) {
            activeNames.value = curManufacturers.value?.[0]?.id;
        }
    }
}, { deep: true, immediate: true });

//收起两边工具
const handlePack = () => {
    show.value = !show.value
    emits('pack')
}

//左侧模型与对象切换
const handleLeftTabs = (e) => {
    leftTabs.value = e;
}

const handleToolbarDragStart = (node, event) => {
    emits('dragStart', node, event)
}

//拖拽结束
const handleToolbarDragEnd = (event) => {
    emits('dragEnd', event)
}

const showInputChange = () => {
    showInput.value = true
}

/**
 * 模型列表点击事件
 */
const nodeClick = (item) => {
    emits('nodeClick', item)
}
//设备分类点击事件
const handleTypeClick = (index) => {
    selectedType.value = index;
    curManufacturers.value = props.leftToolbarList[index].equipmentManufactorVos;
    activeNames.value = curManufacturers.value?.[0]?.id;

}
</script>

<style scoped lang="scss">
@keyframes slideInLeft {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}

@keyframes slideOutLeft {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-100%);
  }
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}

@keyframes slideOutRight {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(100%);
  }
}

@keyframes slideInUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes slideOutUp {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(-100%);
  }
}

@keyframes slideInDown {
  from {
    transform: translateY(-100%);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes slideOutDown {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(100%);
  }
}

.slide-in-left {
  animation: slideInLeft 0.5s forwards;
}

.slide-out-left {
  animation: slideOutLeft 0.5s forwards;
}

.slide-in-right {
  animation: slideInRight 0.5s forwards;
}

.slide-out-right {
  animation: slideOutRight 0.5s forwards;
}

.slide-in-up {
  animation: slideInUp 0.5s forwards;
}

.slide-out-up {
  animation: slideOutUp 0.5s forwards;
}

.slide-in-down {
  animation: slideInDown 0.5s forwards;
}

.slide-out-down {
  animation: slideOutDown 0.5s forwards;
}
.slide-enter-active,
.slide-leave-active {
    transition: all 0.1s ease-out;
    overflow: hidden;
}

.slide-enter-from,
.slide-leave-to {
    max-height: 0;
    opacity: 0;
}

.slide-enter-to,
.slide-leave-from {
    max-height: 500px;
    opacity: 1;
}

// .slide-enter-active {
//   transition: all 0.3s ease-out;
// }
// .slide-leave-active {
//   transition: all 0.2s ease-in;
// }
// .slide-enter-from {
//   transform: translateY(-100%);
//     max-height: 0;
//   opacity: 0;
// }
// .slide-leave-to {
//   transform: translateY(100%);
//     max-height: 500px;
//   opacity: 1;
// }
/* 垂直展开动画 */
// .slide-vertical-enter-active,
// .slide-vertical-leave-active {
//     transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1),
//         opacity 0.2s linear;
//     overflow: hidden;
// }

// .slide-vertical-enter-from,
// .slide-vertical-leave-to {
//     max-height: 0;
//     opacity: 0;
// }

// .slide-vertical-enter-to,
// .slide-vertical-leave-from {
//     max-height: 500px; /* 大于实际内容高度 */
//     opacity: 1;
// }
.left {
    position: absolute;
    top: 18px;
    left: 18px;
    z-index: 100;
    height: calc(100vh - 36px);
    width: 384px;
    border-radius: 10px;
    background: rgba(0, 17, 46, 0.3);
    backdrop-filter: blur(8px);
    &-list {
        display: flex;
        flex-direction: column;
        align-items: center;
        max-height: calc(100vh - 140px);
        overflow-y: auto;

        &-item {
            cursor: pointer;
            margin: 6px 10px;
            width: 341px;
            height: 30px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 0 14px;
            border-radius: 4px;
            color: #fff;
            font-size: 14px;
            &:hover {
                border-radius: 4px;
                background: #13325a;
            }
        }

        &-active {
            cursor: pointer;
            margin: 10px;
            width: 341px;
            height: 30px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 0 14px;
            border-radius: 4px;
            border-radius: 4px;
            background: #0b3b78;
            color: #fff;
            font-size: 14px;
        }
    }
    &-tabs {
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 140px;
        padding-left: 30px;
        &-item {
            cursor: pointer;
            color: #bcbcbc;
            font-size: 16px;
            padding: 2px 6px;

            &:hover {
                border-radius: 2px;
                background: #23446f;
            }
        }
        &-active {
            cursor: pointer;
            color: #fff;
            font-size: 16px;
            padding: 2px 6px;

            &:hover {
                border-radius: 2px;
                background: #23446f;
            }
        }
    }
    &-header {
        display: flex;
        width: 100%;
        align-items: center;
        height: 50px;
        z-index: 100;
        padding: 0 20px;
        position: relative;
        &-back {
            width: 18px;
            height: 18px;
            cursor: pointer;
            &:active {
                opacity: 0.8;
            }
        }
        &-save {
            margin-left: 30px;
            width: 18px;
            height: 18px;
            cursor: pointer;
            &:active {
                opacity: 0.8;
            }
        }
        &-pack {
            margin-left: 20px;
            padding: 4px 6px;
            cursor: pointer;
            &:active {
                opacity: 0.8;
            }
            &:hover {
                border-radius: 4px;
                background: #23446f;
            }

            img {
                width: 18px;
                height: 18px;
            }
        }
        &-input {
            margin-left: 20px;
            width: 220px;
            color: #fff;
        }

        .name {
            cursor: pointer;
            margin-left: 20px;
            width: 220px;
            margin-top: 10px;
            color: #fff;
            font-size: 14px;

            padding: 4px 6px;
            &:hover {
                border-radius: 4px;
                background: #23446f;
            }
        }

        :deep(.el-input__inner) {
            background: transparent !important;
            color: #fff;
            font-size: 14px;
        }
        :deep(.el-input__wrapper) {
            background: transparent !important;
        }
    }
    .model {
        display: flex;
        max-height: calc(100vh - 140px);
        overflow-y: auto;
        &-equipment {
            width: 290px;
            :deep(.el-collapse-item__arrow) {
                color: #fff;
            }

            :deep(.el-collapse-item__wrap) {
                border-bottom: 1px solid #516473 !important;
            }

            :deep(.el-collapse-item__header) {
                border-bottom: 1px solid #516473 !important;
                background: transparent !important;
                color: #fff !important;
                padding-left: 20px;
            }

            :deep(.el-collapse-item) {
                background: transparent !important;
                border-bottom: none !important;
            }
            :deep(.el-collapse) {
                border-bottom: none !important;
            }
            :deep(.el-collapse-item__wrap) {
                background: transparent !important;
                border-top: none;
            }
            &-list {
                width: 100%;
                display: flex;
                flex-wrap: wrap;
                gap: 10px;
                padding: 10px 20px;
                justify-content: space-between;
                .node-item {
                    margin: 4px 0px;
                    flex-direction: column;
                    align-items: center;
                    cursor: pointer;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    cursor: move;
                    transition: all 0.2s;
                    user-select: none;
                    .img {
                        overflow: hidden;
                        height: 110px;
                        width: 114px;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        border-radius: 6px;
                        background: rgba(166, 186, 208, 0.1);
                        box-sizing: border-box;
                        border: 1px solid #ffffff;
                        backdrop-filter: blur(2px);
                        img {
                            overflow: hidden;
                            height: 60px;
                            width: 60px;
                            user-drag: none; /* 标准属性 */
                            -webkit-user-drag: none; /* Safari兼容 */
                        }
                    }
                    .name {
                        height: 20px;
                        display: flex;
                        justify-content: center;
                        overflow: hidden;
                        width: 100%;
                        margin-top: 4px;
                        font-size: 12px;
                        color: #fff;
                    }
                    &:hover {
                        opacity: 0.7;
                        transform: translateX(2px);
                    }
                }
            }
        }
        &-type {
            display: flex;
            flex-direction: column;
            width: 90px;
            gap: 10px;
            padding: 14px 0;
            box-sizing: border-box;
            border-right: #516473 solid 1px;
            max-height: calc(100vh - 140px);
       
            &-item {
                position: relative;
                width: 90px;
                cursor: pointer;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                color: #333;
                font-size: 16px;
                box-sizing: border-box;
                // background: #fff;
                
                &:hover {
                    opacity: 0.7;
                }
                .img {
                    border-radius: 2px;
                    overflow: hidden;
                    height: 34px;
                    width: 34px;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    user-drag: none; /* 标准属性 */
                    -webkit-user-drag: none; /* Safari兼容 */
                    cursor: not-allowed !important;
                    img {
                        height: 24px;
                        width: 24px;
                    }
                }
                .name {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    overflow: hidden;
                    // width: 90px;
                    margin-top: 4px;
                    font-size: 12px;
                    color: #bcbcbc;
                    margin-left: 0;
                }
            }
            &-active {
                position: relative;
                width: 90px;
                cursor: pointer;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                color: #fff;
                font-size: 16px;
                color: #409eff;
                box-sizing: border-box;
                &:hover {
                    opacity: 0.7;
                }

                .img {
                    overflow: hidden;
                    height: 34px;
                    width: 34px;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    border-radius: 6px;
                    background: #23446f;
                    img {
                        height: 24px;
                        width: 24px;
                        filter: drop-shadow(#409eff -40px 0px 0px);
                        transform: translateX(40px);
                        user-drag: none; /* 标准属性 */
                        -webkit-user-drag: none; /* Safari兼容 */
                        cursor: not-allowed !important;
                    }
                }
                .name {
                    margin-top: 4px;
                    font-size: 14px;
                    color: #fff;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    overflow: hidden;
                    margin-left: 0;
                    // width: 90px;
                }
            }
        }
    }
}

/* 整个滚动条 */
::-webkit-scrollbar {
    width: 6px; /* 垂直滚动条宽度 */
    height: 6px; /* 水平滚动条高度 */
}

/* 滚动条轨道（背景） */
::-webkit-scrollbar-track {
    background: #001235;
    border-radius: 6px;
}

/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
    background: #23446f;
    border-radius: 6px;
    // border: 2px solid #001235; /* 滑块与轨道间的间隙 */
}

/* 滑块悬停效果 */
::-webkit-scrollbar-thumb:hover {
    background: #75a0c4;
}

/* 滚动条按钮（上下箭头） */
::-webkit-scrollbar-button {
    display: none; /* 隐藏箭头按钮 */
}
</style>